#!/bin/bash

function site_modules() {
	if [ ! -d $OUTDIR ]
	then
		mkdir -p $OUTDIR
	fi
	
	echo "Listing modules $UNPACKED"
	cd $UNPACKED

	drush pm-list --no-core --status=enabled --type=module --pipe | sort > $OUTDIR/$SITENAME-enabled.log 2>&1
	drush pm-list --no-core --status=disabled --type=module --pipe | sort > $OUTDIR/$SITENAME-disabled.log 2>&1
	drush pm-list --no-core --status='not installed' --type=module --pipe | sort > $OUTDIR/$SITENAME-notused.log 2>&1
}

function site_specific_modules() {
	echo "Listing custom modules $UNPACKED"
	cd $UNPACKED
	
	for subdir in `ls $UNPACKED/modules`
	do
		ls $UNPACKED/modules/$subdir > $OUTDIR/$SITENAME-specific-$subdir.log 2>&1
	done
}

function site_profile() {
	if [ ! -d $OUTDIR ]
	then
		mkdir -p $OUTDIR
	fi
	
	echo "Listing installation profile $UNPACKED"
	cd $UNPACKED

	drush vget install_profile > $OUTDIR/$SITENAME-profile.log 2>&1
}


if [ $# -eq 2 ]
then
	SITENAME=$1
	PLATFORM=$2
	
	if [ ! -d /var/www/$PLATFORM ]
	then
		echo "Platform $PLATFORM not found"
		exit 1
	fi
	
	UNPACKED=/var/www/$PLATFORM/sites/$SITENAME
	OUTDIR=/vagrant/output/modules
	
	site_modules
	site_specific_modules
	site_profile
else
	echo "Usage: site_modules.sh <fqdn_site> <platform_name>"
	exit -1
fi
